// 日历相关API
import type { Data } from './index'

// 学习日历数据接口
export interface StudyCalendar {
  id: number
  userId: number
  eventTitle: string
  eventDate: string
  eventTime: string
  createdAt: string
  completed?: boolean
  color?: string
  notes?: string
}

/**
 * 查询学习日历
 * 获取用户的学习日历数据
 */
export const queryStudyCalendar = (): Promise<UniApp.RequestSuccessCallbackResult> => {
  const token = uni.getStorageSync('token')
  return uni.request({
    url: '/api/studycalendar/query',
    method: 'GET',
    header: {
      token: token,
    },
  })
}

/**
 * 添加学习日历事件
 * @param event 日历事件数据
 */
export const addStudyCalendarEvent = (event: {
  userId: number
  eventTitle: string
  eventDate: string
  eventTime: string
}): Promise<UniApp.RequestSuccessCallbackResult> => {
  const token = uni.getStorageSync('token')
  console.log('添加日历事件，请求URL: /api/studycalendar/add')
  console.log('请求体:', JSON.stringify(event))
  console.log('携带token:', token)

  // 这里根据实际后端接口调整URL
  return uni.request({
    url: '/api/studycalendar/add',
    method: 'POST',
    data: event,
    header: {
      token: token,
      'Content-Type': 'application/json',
    },
  })
}

/**
 * 更新学习日历事件
 * @param event 日历事件数据
 */
export const updateStudyCalendarEvent = (event: {
  id: number
  eventTitle?: string
  eventDate?: string
  eventTime?: string
  completed?: boolean
  color?: string
  notes?: string
}): Promise<UniApp.RequestSuccessCallbackResult> => {
  const token = uni.getStorageSync('token')
  return uni.request({
    url: '/api/studycalendar/update',
    method: 'PUT',
    data: event,
    header: {
      token: token,
      'Content-Type': 'application/json',
    },
  })
}

/**
 * 删除学习日历事件
 * @param id 事件ID
 */
export const deleteStudyCalendarEvent = (
  id: number,
): Promise<UniApp.RequestSuccessCallbackResult> => {
  const token = uni.getStorageSync('token')
  return uni.request({
    url: `/api/studycalendar/delete/${id}`,
    method: 'POST',
    header: {
      token: token,
    },
  })
}
